/*
 * @Author: your name
 * @Date: 2022-01-26 10:17:34
 * @LastEditTime: 2022-04-02 11:05:42
 * @LastEditors: your name
 * @Description: 老师列表页面
 * @FilePath: \web\src\router\adminUserPage\adminChild\teacherList.jsx
 */

import React, {useEffect, useState} from "react";
import { Space, Table } from "antd";
import request from "../../../network/index";
import AdminBox from "../../../components/adminBox";
import { useNavigate } from "react-router-dom";
const TeacherList = () => {
  
  const navigate = useNavigate();
  const [list, setList] = useState([]);
  const [col, setCol] = useState({
    selectedRowKeys: [], // Check here to configure the default column
    loading: false,
  });

  useEffect( () => {
    const getList = async() => {
      const {list} = await request.get('/admin?type=2');
      list && list.map(e => e.key = e._id);
      setList(list);
    }
    getList()
  },[])

  const columns = [
    {
      title: "工号",
      dataIndex: "id",
      key: "_id"
    },
    {
      title:"学院",
      dataIndex: "academy"
    },
    {
      title: "姓名",
      dataIndex: "username"
    },
    {
      title: "操作",
      key: "action",
      render: (text, record) => (
        <Space size="middle">
          <span onClick={() => onEdit(record)}>修改</span>
          <span onClick={() => onDelete(record._id)}>删除</span>
        </Space>
      ),
    },
  ];
  const onSelectChange = selectedRowKeys => {
    console.log('selectedRowKeys changed: ', selectedRowKeys);
    setCol({ selectedRowKeys });
  };

  const onEdit = item => {
    navigate("/adminUserPage/teacherCreate",{state : item});
  };
  const onDelete = async id => {
    await request.delete(`/admin/${id}?type=2`)
  };

  return (
    <div id="teacherList">
      <AdminBox title="学生列表">
        <Table
          columns={columns}
          dataSource={list}
          position="bottomCenter"
          pagination={{ pageSize: 5 }}
          rowSelection={{selectedRowKeys: col.selectedRowKeys,onChange: onSelectChange}}
        />
      </AdminBox>
    </div>
  );
};

export default TeacherList;
